package com.lottery.system.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
 * @author: xiaoxie
 * create: 2024-09-04 09:40
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.common.utils
 * description: redis操作的工具包
 */
@Configuration
public class RedisUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private final static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
    /**
     * @description: 设置值(String,String)
     * @author: xiaoxie
     * @date: 2024/9/4 9:44
     * @param: [key, value]
     * @return: boolean
     **/
    public boolean set(String key, String value) {
        try {
            stringRedisTemplate.opsForValue().set(key, value);
            return true;
        }catch (Exception e) {
            logger.error("RedisUtil error,set({},{})",key,value,e);
            return false;
        }
    }
    /**
     * @description: 设置值,并且设置过期时间,单位为秒
     * @author: xiaoxie
     * @date: 2024/9/4 9:46
     * @param: [key, value, time]
     * @return: boolean
     **/
    public boolean setTimeout(String key, String value,Long time) {
        try {
            //单位为秒
            stringRedisTemplate.opsForValue().set(key, value,time, TimeUnit.SECONDS);
            return true;
        }catch (Exception e) {
            logger.error("RedisUtil error,setTimeout({},{},{})",key,value,time,e);
            return false;
        }
    }
    /**
     * @description: 获取value
     * @author: xiaoxie
     * @date: 2024/9/4 10:12
     * @param: [key]
     * @return: java.lang.String
     **/
    public String get(String key) {
        try {
            return StringUtils.hasText(key) ?
                    stringRedisTemplate.opsForValue().get(key)
                    : null;
        }catch (Exception e) {
            logger.error("RedisUtil error,get({})",key,e);
            return null;
        }
    }
    /**
     * @description: 删除key,可以删除多个.
     * @author: xiaoxie
     * @date: 2024/9/4 10:21
     * @param: [key] String,可多个
     * @return: boolean
     **/
    public boolean del(String... key) {
        try {
            if(key != null && key.length > 0) {
                if(key.length == 1) {
                    stringRedisTemplate.delete(key[0]);
                }else {
                    stringRedisTemplate.delete(
                            (Collection<String>) CollectionUtils.arrayToList(key)
                    );
                }
                return true;
            }return false;
        }catch (Exception e) {
            logger.error("RedisUtil error,del({})",key,e);
            return false;
        }
    }
    /**
     * @description: 查看key是否存在.
     * @author: xiaoxie
     * @date: 2024/9/4 10:27
     * @param: [key]
     * @return: java.lang.Boolean
     **/
    public Boolean hasKey(String key) {
        try {
            return StringUtils.hasText(key) ?
                    stringRedisTemplate.hasKey(key)
                    : Boolean.FALSE;
        }catch (Exception e) {
            logger.error("RedisUtil error, hasKey({})", key, e);
            return Boolean.FALSE;
        }
    }
}
